package org.luxor.commons.rabbitmq.service.workqueue;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听死信队列
 *
 * @author Mr.Yan  @date 2019/10/18$
 */
@RabbitListener(bindings = @QueueBinding(
        exchange = @Exchange(value = "dead.exchange", type = "fanout"),
        value = @Queue("dead.queue")
))
@Component
public class DeadQueueListener {
    private static final Logger log = LoggerFactory.getLogger(DeadQueueListener.class);

    @RabbitHandler
    public void oneConsumeError(String message, Channel channel,
                                @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        log.error("<<< [ 死信队列 ] 收到一条处理失败的记录. msg:{}", message);
        // 确认消息
        channel.basicAck(deliveryTag, false);
    }

}
